﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class Server : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["searchText"] != null)
            {
                if (Request.QueryString["searchText"].ToString().Trim().Length > 0)
                {
                    #region
                    V1DataContext DB = new V1DataContext();
                    //var query=(from A in DB.authors.Where(b=>b.authorName.StartsWith("张"))
                    //        group A by A.authorName into g
                    //        let c=g.Count()
                    //         orderby c desending
                    //      select new {authorName=g.Key,cnt=c}).Take(5);
                    //var queryAU = (from A in DB.authors
                    //               group A by new { authorName = A.authorName } into g
                    //               select new { authorName = g.Key.authorName, nameCount = g.Count() } into gs
                    //               where gs.authorName.StartsWith("张")
                    //               orderby gs.nameCount descending
                    //               select gs.authorName).Distinct().Take(5);
                    var queryName = (from A in DB.authors
                                     group A by new
                                     {
                                         authorName = A.authorName
                                     } into grA
                                     select new
                                     {
                                         authorName = grA.Key.authorName,
                                         Quantity = grA.Count()
                                     } into orderbygrA
                                     where orderbygrA.authorName.StartsWith(Request.QueryString["searchText"].Trim())
                                     orderby orderbygrA.Quantity descending
                                     select orderbygrA.authorName).Distinct().Take(7);
                    System.Text.StringBuilder returnText = new System.Text.StringBuilder(100);
                    foreach (var item in queryName)
                    {
                        returnText.Append(item.ToString() + "\n");
                    }
                    Response.Write(returnText.ToString());


                    //DataTable dt = new DataTable();
                    //using (SqlConnection conn = new SqlConnection(
                    //    System.Configuration.ConfigurationManager.AppSettings["ConnStr"]))
                    //{
                    //    SqlCommand cmd = new SqlCommand();
                    //    cmd.Connection = conn;
                    //    cmd.CommandText = string.Format(
                    //        "Select BookName From Books Where BookName like '%{0}%'",
                    //        Request.QueryString["searchText"]);
                    //    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    //    conn.Open();
                    //    adapter.Fill(dt);
                    //}
                    //string returnText = "";
                    //if (dt != null && dt.Rows.Count > 0)
                    //{
                    //    for (int i = 0; i < dt.Rows.Count; i++)
                    //    {
                    //        returnText += dt.Rows[i][0].ToString() + "\n";
                    //    }
                    //}
                    //Response.Write(returnText);
                    #endregion
                }
            }
        }
    }
}